import 'package:flutter/cupertino.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:io';

/// 权限
/// denied=>没有权限,可以发起申请
/// permanentlyDenied=>没有权限,永久拒绝的,只能通过打开设置去开启权限
/// restricted=>受限制的权限,比如家长控制权限
/// granted=>有权限
enum PermissionUtilsStatus { denied, permanentlyDenied, restricted, granted }

class PermissionUtils {
  static PermissionUtils? _instance;

  static PermissionUtils getInstance() {
    if (_instance == null) {
      _instance = new PermissionUtils._();
    }
    return _instance!;
  }

  PermissionUtils._();

  /// 判断是否有权限
  /// 状态进行了简化，统一
  Future<Map<Permission, PermissionUtilsStatus>> has(
      {@required List<Permission>? permissions}) async {
    assert(permissions != null && permissions.length > 0);
    Map<Permission, PermissionUtilsStatus> resultPermissions = {};
    for (int i = 0, len = permissions!.length; i < len; i++) {
      var permission = permissions[i];
      var status = await permission.status;
      if (status == PermissionStatus.denied) {
        if (Platform.isIOS) {
          resultPermissions[permission] =
              PermissionUtilsStatus.permanentlyDenied;
        } else {
          resultPermissions[permission] = PermissionUtilsStatus.denied;
        }
      } else if (status == PermissionStatus.restricted) {
        resultPermissions[permission] = PermissionUtilsStatus.restricted;
      } else if (status == PermissionStatus.granted) {
        resultPermissions[permission] = PermissionUtilsStatus.granted;
      } else if (status == PermissionStatus.permanentlyDenied) {
        resultPermissions[permission] = PermissionUtilsStatus.permanentlyDenied;
      }
    }
    return resultPermissions;
  }

  /// 判断是否有权限
  Future<PermissionUtilsStatus> hasOne(Permission permission) async {
    var res = await has(permissions: [permission]);
    return Future.value(res[permission]);
  }

  /// 请求权限
  /// 状态进行了简化，统一
  Future<Map<Permission, PermissionUtilsStatus>> request(
      {@required List<Permission>? permissions}) async {
    assert(permissions != null && permissions.length > 0);
    Map<Permission, PermissionUtilsStatus> resultPermissions = {};
    Map<Permission, PermissionStatus> resultMap = await permissions!.request();
    for (int i = 0, len = permissions.length; i < len; i++) {
      var permission = permissions[i];
      var status = resultMap[permission];
      if (status == PermissionStatus.denied) {
        if (Platform.isIOS) {
          resultPermissions[permission] =
              PermissionUtilsStatus.permanentlyDenied;
        } else {
          resultPermissions[permission] = PermissionUtilsStatus.denied;
        }
      } else if (status == PermissionStatus.restricted) {
        resultPermissions[permission] = PermissionUtilsStatus.restricted;
      } else if (status == PermissionStatus.granted) {
        resultPermissions[permission] = PermissionUtilsStatus.granted;
      } else if (status == PermissionStatus.permanentlyDenied) {
        resultPermissions[permission] = PermissionUtilsStatus.permanentlyDenied;
      }
    }
    return resultPermissions;
  }

  /// 请求权限
  Future<PermissionUtilsStatus> requestOne(Permission permission) async {
    var res = await request(permissions: [permission]);
    return Future.value(res[permission]);
  }

  /// 打开app设置页面
  Future<bool> openAppSetting() {
    return openAppSettings();
  }
}
